
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

import 'baseController.dart';


class AppRouteObserver {
  //这是实际上的路由监听器
  static final RouteObserver<ModalRoute<void>> _routeObserver =
  RouteObserver<ModalRoute<void>>();
  //这是个单例
  static final AppRouteObserver _appRouteObserver =
  AppRouteObserver._internal();

  AppRouteObserver._internal() {}
  //通过单例的get方法轻松获取路由监听器
  RouteObserver<ModalRoute<void>> get routeObserver {
    return _routeObserver;
  }

  factory AppRouteObserver() {
    return _appRouteObserver;
  }
}


abstract class SEPageCountDownState<T extends StatefulWidget> extends SEPageState<T> {

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }


  @override
  void dispose() {
    // TODO: implement dispose
    /// 取消路由订阅
    ///
    super.dispose();
  }


  @override
  void didPush() {
    // TODO: implement didPush
    super.didPush();
  }

  @override
  void didPop() {
    // TODO: implement didPop
    super.didPop();
  }
  @override
  void didPushNext() {
    // TODO: implement didPushNext
    super.didPushNext();
  }

  @override
  void didPopNext() {
    // TODO: implement didPopNext
    super.didPopNext();
  }

  onBack(){

  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return super.build(context);
  }
}


abstract class SEPageState<T extends StatefulWidget> extends SEContentState<T> {

  BaseController controller = BaseController();

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('${widget.runtimeType.toString()} initState');

  }


  @override
  void dispose() {
    // TODO: implement dispose
    /// 取消路由订阅
    super.dispose();
    print('${widget.runtimeType.toString()} dispose');
  }


  /// 当前的页面被push显示到用户面前 viewWillAppear.
  @override
  void didPush() {
    super.didPush();
    print('${widget.runtimeType.toString()} didPush');

  }


  /// 当前的页面被pop viewWillDisappear.
  @override
  void didPop() {
    super.didPop();
    print('${widget.runtimeType.toString()} didPop');

  }


  /// 上面的页面被pop后当前页面被显示时 viewWillAppear.
  @override
  void didPopNext() {
    super.didPopNext();
    print('${widget.runtimeType.toString()} didPopNext');
  }


  /// 从当前页面push到另一个页面 viewWillDisappear.
  @override
  void didPushNext() {
    super.didPushNext();
    print('${widget.runtimeType.toString()} didPushNext');

  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return super.build(context);
  }
}


abstract class SEContentState<T extends StatefulWidget> extends State<T>
    with RouteAware,AutomaticKeepAliveClientMixin {
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    /// 路由订阅
    AppRouteObserver().routeObserver.subscribe(this, ModalRoute.of(context)!);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // print('initState ${widget.runtimeType.toString()}');
  }


  @override
  void dispose() {
    // TODO: implement dispose
    /// 取消路由订阅
    AppRouteObserver().routeObserver.unsubscribe(this);
    super.dispose();
    print('dispose ${widget.runtimeType.toString()}');

  }


  /// 当前的页面被push显示到用户面前 viewWillAppear.
  @override
  void didPush() {
    super.didPush();
    print('didPush ${widget.runtimeType.toString()}');

  }


  /// 当前的页面被pop viewWillDisappear.
  @override
  void didPop() {
    super.didPop();
    // print('didPop ${widget.runtimeType.toString()}');

  }


  /// 上面的页面被pop后当前页面被显示时 viewWillAppear.
  @override
  void didPopNext() {
    super.didPopNext();
    // print('didPopNext ${widget.runtimeType.toString()}');

  }


  /// 从当前页面push到另一个页面 viewWillDisappear.
  @override
  void didPushNext() {
    super.didPushNext();
    // print('didPushNext ${widget.runtimeType.toString()}');

  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    AppPageBuildRefresh.instance.refreshAction?.call(context);
    return super.build(context);
  }
}

class AppPageBuildRefresh {
    static final _instance = AppPageBuildRefresh();
    static AppPageBuildRefresh get instance {
      return _instance;
    }
    Function(BuildContext context)? refreshAction;
  }


